package com.gym.code.config.admin.service.generate.impl;

import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.gym.code.config.admin.bean.db.DBBaseMappingInfo;
import com.gym.code.config.admin.bean.table.TableMappingBean;
import com.gym.code.config.admin.service.IDBMappingService;
import com.gym.code.config.admin.service.ITableMappingService;
import com.gym.code.config.admin.service.generate.ICodeFileGenerateService;
import com.gym.code.generator.impl.ModuleGenerator;
import com.gym.code.generator.model.mapping.ModuleGenerateModel;

/**
 * 
 * @author xinchun.wang 
   @email: 532002108@qq.com
 * @createTime 2015-5-1 下午12:43:24
 */
@Service
public class CodeFileGenerateService implements ICodeFileGenerateService {
	private static final Logger logger = LoggerFactory.getLogger(CodeFileGenerateService.class);
	private static ModuleGenerator moduleGenerator = ModuleGenerator.getInstance();
	
	private static final Map<String,Object> SUCCESS_RESULT = new HashMap<String,Object>();
	static {
		SUCCESS_RESULT.put("ret", true);
		SUCCESS_RESULT.put("msg", "操作成功");
	}
	
	@Autowired
	private ITableMappingService tableMappingService;
	
	@Autowired
	private IDBMappingService dbMappingService;
	
	@Override
	public Map<String, Object> genTable(long tableId) {
		TableMappingBean tableBean = tableMappingService.query(tableId);
		DBBaseMappingInfo dbBean = dbMappingService.queryBase(tableBean.getDbId());
		ModuleGenerateModel model = BeanModelConvertUtils.convert(dbBean, tableBean);
		try {
			moduleGenerator.generate(model);
		}catch(Exception e){
			logger.error("genTable {}",tableId,e);
			Map<String,Object> result = new HashMap<String,Object>();
			result.put("ret", false);
			result.put("msg", e);
			return result;
		}
		return SUCCESS_RESULT;
	}

	
	
	
	@Override
	public Map<String, Object> genDB(long dbId) {
		// TODO Auto-generated method stub
		return null;
	}

}
